﻿using Microsoft.VisualStudio.TestTools.UITest.Common;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Microsoft.VisualStudio.TestTools.UITesting;
using System;
using System.Linq;

namespace AXExtension.Filters
{
	/// <summary>
	/// This is by far the most complicated filter. This is
	/// caused by the extremely limited functionality given by the
	/// action stack when it comes to UI elements. Search is
	/// impossible from the existing action, so I have to search
	/// downwards from the AX window itself.
	/// </summary>
	class NavigationPaneFilter : Filter
	{

		public NavigationPaneFilter()
		{
			Require(stack => stack.Peek().UIElement.ControlTypeName == "TreeItem");
		}

		public override void Run(IUITestActionStack stack)
		{
			// If it has any children, this is just an organizational
			// TreeItem, so we can ignore that action.
			if (Playback.GetCoreTechnologyManager("MSAA").GetChildren(stack.Peek().UIElement, null).MoveNext())
			{
				stack.Pop();
				return;
			}

			UITestAction action = stack.Peek();
			UITechnologyElement toolbar = GetToolbar(action);

			var mouse = new MouseAction(toolbar,
				System.Windows.Forms.MouseButtons.Left,
				MouseActionType.Click);
			// The farthest right location on the toolbar.
			mouse.Location = new System.Drawing.Point(835, 5);

			string companyName = 
				stack.Peek().UIElement.TopLevelElement.Name.OnlyNormals().Between("[ - ", " - initial]").ToUpper();
			AXUtils.LoadManager("MSAA");

			var keys = new SendKeysAction();
			keys.Text = companyName + PathRepresented(action.UIElement) + "{Enter}";
			stack.Pop();
			// If the user set the area page the newly pushed actions will do that anyway.
			if (stack.Count >= 2 &&
				stack.Peek(1).UIElement.ControlTypeName == "SplitButton")
			{
				stack.Pop();
				stack.Pop();
			}
			stack.Push(mouse);
			stack.Push(keys);
		}

		

		private UITechnologyElement GetToolbar(UITestAction action)
		{
			AXUtils.LoadManager("UIA");
			IUITechnologyElement parent = AXUtils.Desktop
				.Child("Dynamics", "Window")
				.Child("WindowHeaderFrame", "Pane")
				.Child("Pane")
				.Child("TopRow", "Pane")
				.Child("AddressBarContainer", "Pane");

			IUITechnologyElement toolbar = parent.Child("ToolBar");

			toolbar.QueryId.Ancestor = new UITechnologyElementRedirect(parent,
				action.UIElement.TopLevelElement,
				Playback.GetCoreTechnologyManager("MSAA"), "MSAA");
			toolbar.QueryId.Condition = new AndCondition(
				new PropertyCondition("ControlType", "ToolBar"));
			parent.QueryId.Condition = new AndCondition(
				new PropertyCondition("ClassName", "WindowsForms10.Window", PropertyConditionOperator.Contains),
				new PropertyCondition("Instance", "35"),
				new PropertyCondition("ControlType", "Window"));
			return new UITechnologyElementRedirect(toolbar,
				action.UIElement.TopLevelElement,
				Playback.GetCoreTechnologyManager("MSAA"), "MSAA");
		}

		private string PathRepresented(IUITechnologyElement element)
		{
			if (element == null ||
				element.ControlTypeName == "Tree")
			{
				return "";
			}
			return PathRepresented(element.Parent()) + "/" + element.Name;
		}
	}
}
